package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.domain.bo.MallGoodsBo;
import com.ruoyi.mall.domain.bo.MallGoodsCategoryBo;
import com.ruoyi.mall.domain.vo.MallGoodsCategoryVo;
import com.ruoyi.mall.domain.vo.MallGoodsVo;
import com.ruoyi.mall.service.IMallGoodsService;
import com.ruoyi.system.domain.bo.SysOssBo;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.MallGoodsCategory;
import com.ruoyi.mall.mapper.MallGoodsCategoryMapper;
import com.ruoyi.mall.service.IMallGoodsCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 商城商品分类信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class MallGoodsCategoryServiceImpl implements IMallGoodsCategoryService {

    private final MallGoodsCategoryMapper baseMapper;

    /**
     * 查询商城商品分类信息
     */
    @Override
    public MallGoodsCategoryVo queryById(Long categoryId){
        return baseMapper.selectVoById(categoryId);
    }

    /**
     * 查询商城商品分类信息列表
     */
    @Override
    public TableDataInfo<MallGoodsCategoryVo> queryPageList(MallGoodsCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallGoodsCategory> lqw = buildQueryWrapper(bo);
        Page<MallGoodsCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商城商品分类信息列表
     */
    @Override
    public List<MallGoodsCategoryVo> queryList(MallGoodsCategoryBo bo) {
        LambdaQueryWrapper<MallGoodsCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<MallGoodsCategoryVo> getGoodsList()
    {
        MallGoodsCategoryBo bo = new MallGoodsCategoryBo();
        LambdaQueryWrapper<MallGoodsCategory> lqw = buildQueryWrapper(bo);
        List<MallGoodsCategoryVo> lstMallGoodsCategory = baseMapper.selectVoList(lqw);
//        MallGoodsBo mallGoodsBo = new MallGoodsBo();
//        List<MallGoodsVo> mallGoodsList = mallGoodsService.queryList(mallGoodsBo);
//        SysOssBo sysOssBo = new SysOssBo();
//        List<SysOssVo> sysOssVoList = sysOssService.queryList(sysOssBo);
//        for (com.ruoyi.mall.domain.vo.MallGoodsCategoryVo mallGoodsCategoryVo: lstMallGoodsCategory)
//        {
//            SysOssVo goodsCategoryOssVo = sysOssVoList.stream().filter(item -> item.getOssId().equals(mallGoodsCategoryVo.getCategoryImg())).findFirst().orElse(null);
//            mallGoodsCategoryVo.setCategoryImgUrl(goodsCategoryOssVo.getUrl());
//            List<MallGoodsVo> lstGoods = mallGoodsList.stream().filter(item -> item.getCategoryId().equals(mallGoodsCategoryVo.getCategoryId())).collect(Collectors.toList());
//            for (MallGoodsVo mallGoodsVo : lstGoods)
//            {
//                SysOssVo goodsOssVo = sysOssVoList.stream().filter(item -> item.getOssId().equals(mallGoodsVo.getGoodsImg())).findFirst().orElse(null);
//                mallGoodsVo.setGoodsImgUrl(goodsOssVo.getUrl());
//                mallGoodsCategoryVo.getLstGoods().add(mallGoodsVo);
//            }
//        }
        return lstMallGoodsCategory;
    }

    private LambdaQueryWrapper<MallGoodsCategory> buildQueryWrapper(MallGoodsCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallGoodsCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), MallGoodsCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getParentId() != null, MallGoodsCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getCategoryImg() != null, MallGoodsCategory::getCategoryImg, bo.getCategoryImg());
        lqw.eq(bo.getYn() != null, MallGoodsCategory::getYn, bo.getYn());
        return lqw;
    }

    /**
     * 新增商城商品分类信息
     */
    @Override
    public Boolean insertByBo(MallGoodsCategoryBo bo) {
        MallGoodsCategory add = BeanUtil.toBean(bo, MallGoodsCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改商城商品分类信息
     */
    @Override
    public Boolean updateByBo(MallGoodsCategoryBo bo) {
        MallGoodsCategory update = BeanUtil.toBean(bo, MallGoodsCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallGoodsCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商城商品分类信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
